package cn.edu.flink.scala.tutorial.wordcount

import cn.edu.flink.scala.tutorial.wordcount.source.WordCountSourceFunction
import org.apache.flink.api.java.utils.ParameterTool
import org.apache.flink.streaming.api.scala._

object StreamWordCount {
  def main(args: Array[String]): Unit = {
    // 创建流执行环境
    val env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI()
    env.setParallelism(2)

    // 读取文本文件
    val lineDS = env.addSource(WordCountSourceFunction())

    // 对数据进行格式转换
    val wordAndOne = lineDS.flatMap(_.split(" ")).map(w => (w, 1))
    // 对数据进行分组
    val wordAndOneKS = wordAndOne.keyBy(_._1)
    // 对分组数据进行聚合
    val result = wordAndOneKS.sum(1)
    // 打印结果
    result.print()

    // 执行任务
    env.execute()

  }
}
